#pragma once
#include "../buffer/buffer.h"
#include "http_request.h"
#include "http_response.h"
#include <arpa/inet.h>
#include <memory>
#include <sys/types.h>
#include <sys/uio.h>

namespace zh
{
    class HttpConnection;
    using HttpConnectionSharedPtr = std::shared_ptr<HttpConnection>;

    class HttpConnection
    {
    public:
        HttpConnection()  = default;
        ~HttpConnection() = default;

        void initialize(int socketFD, const sockaddr_in& address);
        void clear();
        void close();

        bool handleConnection();

        ssize_t readBuffer(int& savEerroNo);
        ssize_t writeBuffer(int& savEerroNo);

        const char* getIP() const;
        int         getPort() const;
        int         getFD() const;

        int  getWriteBytes() const;
        bool isKeepAlive() const;

        static std::string s_sourceDirectory;
        static bool        s_isEdgeTrigger;

    private:
        int         m_socketFD {-1};
        sockaddr_in m_address;
        bool        m_isClose {true};

        Buffer m_readBuffer;
        Buffer m_writeBuffer;

        HttpRequest  m_request;
        HttpResponse m_response;

        int          m_iovCount;
        struct iovec m_iov[2];
    };
} // namespace zh